<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="药材名称" prop="medicineName">
        <el-input
          v-model="queryParams.medicineName"
          placeholder="请输入药材名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药材类型" prop="medicineTypeKey">
        <!-- <el-input v-model="queryParams.prescriptionTypeKey" placeholder="请输入药剂类型" clearable size="small"
          @keyup.enter.native="handleQuery" /> -->

        <el-select v-model="queryParams.medicineTypeKey" clearable size="small">
          <el-option v-for="item in typeList" :key="item.medicineTypeKey" :value="item.medicineTypeKey"
            :label="item.medicineTypeName" />

        </el-select>
      </el-form-item>
      <el-form-item label="功效与作用" prop="function">
        <el-input
          v-model="queryParams.function"
          placeholder="请输入功效与作用"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zhongyao:medicine:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zhongyao:medicine:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zhongyao:medicine:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zhongyao:medicine:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="medicineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
     
      <el-table-column label="药材名称" align="center" prop="medicineName" width="75" />
      <el-table-column label="分类" align="center" prop="medicineTypeName"  width="85"/>
      <el-table-column label="药材英文名" align="center" prop="medicineEnglishName"  width="85"/>
      <el-table-column label="用量" align="center" prop="taste"  width="95"/>
      <el-table-column label="图片" align="center" prop="picUrl" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.picUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="别名" align="center" prop="aliasName" />
      <el-table-column label="性状" align="center" prop="medicineCharacter" />
      <el-table-column label="产地" align="center" prop="chanDi" />
      <el-table-column label="使用禁忌" align="center" prop="jinJi" />
    
      <el-table-column label="化学成分" align="center" prop="chengFen" />
      <el-table-column label="功效与作用" align="center" prop="function" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zhongyao:medicine:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zhongyao:medicine:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药材维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="药材名称" prop="medicineName">
          <el-input v-model="form.medicineName" placeholder="请输入药材名称" />
        </el-form-item>
        <el-form-item label="药材英文名" prop="medicineEnglishName">
          <el-input v-model="form.medicineEnglishName" placeholder="请输入药材英文名" />
        </el-form-item>
        <el-form-item label="别名" prop="aliasName">
          <el-input v-model="form.aliasName" placeholder="请输入别名" />
        </el-form-item>
        <el-form-item label="药材类型" prop="medicineTypeKey">
        <!-- <el-input v-model="queryParams.prescriptionTypeKey" placeholder="请输入药剂类型" clearable size="small"
          @keyup.enter.native="handleQuery" /> -->

        <el-select v-model="form.medicineTypeKey" clearable size="small">
          <el-option v-for="item in typeList" :key="item.medicineTypeKey" :value="item.medicineTypeKey"
            :label="item.medicineTypeName" />

        </el-select>
      </el-form-item>

        <!-- <el-form-item label="分类" prop="medicineTypeName">
          <el-input v-model="form.medicineTypeKey" placeholder="请输入分类" />
        </el-form-item> -->
        <el-form-item label="性状" prop="medicineCharacter">
          <el-input v-model="form.medicineCharacter" placeholder="请输入性状" />
        </el-form-item>
        <el-form-item label="图片" prop="picUrl">
          <image-upload :limit="imgLimit" v-model="form.picUrl"/>
        </el-form-item>
        <el-form-item label="产地" prop="chanDi">
          <el-input v-model="form.chanDi" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="用量" prop="taste">
          <el-input v-model="form.taste" placeholder="请输入用量" />
        </el-form-item>
        <el-form-item label="功效与作用" prop="function">
          <el-input v-model="form.function" type="textarea" placeholder="请输入功效与作用" />
        </el-form-item>
        <el-form-item label="化学成分" prop="chengFen">
          <el-input v-model="form.chengFen" placeholder="请输入化学成分" />
        </el-form-item>
        <el-form-item label="使用禁忌" prop="jinJi">
          <el-input v-model="form.jinJi" type="textarea" placeholder="请输入使用禁忌" />
        </el-form-item>
      
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listType } from "@/api/zhongyao/type";
import { listMedicine, getMedicine, delMedicine, addMedicine, updateMedicine } from "@/api/zhongyao/medicine";

export default {
  name: "Medicine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      imgLimit: 1 ,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      typeList:[],
      // 药材维护表格数据
      medicineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        medicineTypeKey:null,
        pageNum: 1,
        pageSize: 10,
        medicineName: null,
        medicineEnglishName: null,
        taste: null,
        function: null,
        aliasName: null,
        medicineCharacter: null,
        chanDi: null,
        jinJi: null,
        picUrl: null,
        chengFen: null,
        deleteFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        medicineName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        medicineEnglishName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        aliasName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        taste: [
          { required: true, message: "此项不能为空", trigger: "blur" },
        ],
        function: [
          { required: true, message: "此项不能为空", trigger: "blur" },
        ],
        picUrl: [
          { required: true, message: "此项不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
    listType().then(response => {
        this.typeList = response.rows;
      
      });
  },
  methods: {
    /** 查询药材维护列表 */
    getList() {
      this.loading = true;
      listMedicine(this.queryParams).then(response => {
        this.medicineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        medicineKey: null,
        medicineName: null,
        medicineEnglishName: null,
        taste: null,
        function: null,
        aliasName: null,
        medicineCharacter: null,
        chanDi: null,
        jinJi: null,
        picUrl: null,
        chengFen: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        deleteFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.medicineKey)
      this.names = selection.map(item => item.medicineName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加药材维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const medicineKey = row.medicineKey || this.ids
      getMedicine(medicineKey).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改药材维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.medicineKey != null) {
            updateMedicine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMedicine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const medicineKeys = row.medicineKey || this.ids;
      const names = row.medicineName || this.names;
      this.$modal.confirm('是否确认删除药材维护编号为"' + names + '"的数据项？').then(function() {
        return delMedicine(medicineKeys);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zhongyao/medicine/export', {
        ...this.queryParams
      }, `medicine_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
